package com.wanwen.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanwen.dto.SearchCon;
import com.wanwen.entity.Role;
import com.wanwen.mapper.RoleMapper;
import com.wanwen.utils.Result;
import com.wanwen.utils.ResultData;
@RestController
@RequestMapping("/role")
public class RoleController {
	@Autowired
	private RoleMapper roleMapper;
	
	 /**多条件分页查询
     * @param search 条件对象
     * @return
     */
    @RequestMapping("/getAllbyLimit")
    public Object showAllByCon(SearchCon search) {
    	//封装条件查询构造器
    	 QueryWrapper<Role> wrapper=new QueryWrapper<Role>();
    	 //封装分页对象
    	Page<Role> page=new Page<Role>(search.getPage(), search.getLimit());
    	Page<Role> userPage = roleMapper.selectPage(page, wrapper);
    	
       return new ResultData(0, "", userPage.getRecords(),userPage.getTotal());
    }
    /**查询所有角色
     * @return
     */
    @RequestMapping("/getAll")
    public Object showAll() {
    	List<Role> selectList = roleMapper.selectList(null);
       return new ResultData(0, "", selectList,null);
    }
	
	/**
	 * /role/add
	 * 添加角色
	 * @param role
	 * @return rs
	 * */
	@RequestMapping("/add")
	public Object add(Role role) {
		QueryWrapper<Role> wrapperone = new QueryWrapper<Role>();
		wrapperone.eq("rname", role.getRname());
		Role roleOne = roleMapper.selectOne(wrapperone);
		if (roleOne == null) {
		Result rs = new Result(role.getId()+"", "", roleMapper.insert(role)==1?true:false);
		return rs;
		}else {
			Result rs = new Result("角色名已存在", "", false);
			return rs;
		}
	}
	/**
	 * /role/update
	 * 修改角色
	 * @param role
	 * @return rs
	 * */
	@RequestMapping("/update")
	public Object update(Role role) {
		QueryWrapper<Role> wrapperone = new QueryWrapper<Role>();
		wrapperone.eq("rname", role.getRname());
		Role roleOne = roleMapper.selectOne(wrapperone);
		if (roleOne == null) {
		Result rs = new Result(role.getId()+"", "", roleMapper.updateById(role)==1?true:false);
		return rs;
		}else {
			Result rs = new Result("角色名已存在", "", false);
			return rs;
		}
	}
	/**
	 * /role/del
	 * 删除角色
	 * @param role
	 * @return rs
	 * */
	@RequestMapping("/del")
	public Object del(Role role) {
		Result rs = new Result(role.getId()+"", "", roleMapper.deleteById(role)==1?true:false);
		return rs;
	}
}
